package cn.terminus.web.customer.custominfo.controller;

import cn.terminus.client.customer.analysis.param.CustomerStageParam;
import cn.terminus.client.customer.analysis.result.CustomerNatureIndustryResultList;
import cn.terminus.client.customer.analysis.result.CustomerStageNumResult;
import cn.terminus.client.customer.custominfo.param.*;
import cn.terminus.client.customer.custominfo.result.ChannelCustomerNumberResult;
import cn.terminus.client.customer.custominfo.result.CustomInfoByNameResult;
import cn.terminus.client.customer.custominfo.result.CustomInfoResult;
import cn.terminus.client.customer.custominfo.service.CustomInfoService;
import cn.terminus.client.customer.userrecordinfo.param.AddUserRecordInfoParam;
import cn.terminus.result.common.exception.ServiceException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.result.TerminusResult;
import cn.terminus.result.common.util.BeanCopyUtils;
import cn.terminus.web.customer.analysis.CustomerStageQuery;
import cn.terminus.web.customer.common.AbstractTerminusController;
import cn.terminus.web.customer.custominfo.form.*;
import cn.terminus.web.customer.userrecordinfo.form.AddUserRecordInfoForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CustomInfo)控制层
 *
 * @author makejava
 * @version 1.0.0
 * @apiDefine CustomInfo $serviceComment
 * @since 2021-07-08 11:18:50
 */
@RestController
@RequestMapping("/customer/customInfo")
@Api(value = "客户管理控制层")
public class CustomInfoController extends AbstractTerminusController {
    /**
     * 服务对象
     */
    @Resource
    private CustomInfoService customInfoService;


    /**
     * 渠道联系人删除后，客户管理渠道联系人字段置空
     */
    @PostMapping("/changeCustomerChannel")
    @ApiOperation(value = "客户管理渠道联系人字段置空", httpMethod = "POST")
    public TerminusResult<List<ChannelCustomerNumberResult>> changeCustomerChannel(@RequestBody CustomerChannelUserForm form){
        CustomerChannelUserParam param = BeanCopyUtils.copyProperties(form, CustomerChannelUserParam.class);
        try {
            return customInfoService.changeCustomerChannel(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("置空失败，原因：", e);
            return TerminusResult.fail("410024", this.getMessage("410024"));
        }
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增客户", httpMethod = "POST")
    public TerminusResult<String> addDetail(@RequestBody AddCustomInfoForm form) {
        AddCustomInfoParam param = BeanCopyUtils.copyProperties(form, AddCustomInfoParam.class);
        try {
            return customInfoService.addDetail(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("新增客户失败，原因：", e);
            return TerminusResult.fail("410002", this.getMessage("410002"));
        }
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "分页条件查询客户", httpMethod = "POST")
    public TerminusResult<TerminusPageAble<CustomInfoResult>> page(@RequestBody CustomInfoQueryForm form) {
        CustomInfoQueryParam param = BeanCopyUtils.copyProperties(form, CustomInfoQueryParam.class);
        try {
            TerminusPageInfo page = new TerminusPageInfo(form.getCurrentPage(), form.getRowsOfPage());
            return customInfoService.page(param, page, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("分页查询客户失败，原因：", e);
            return TerminusResult.fail("410003", this.getMessage("410003"));
        }
    }

    @RequestMapping(value = "/getDetail", method = RequestMethod.POST)
    @ApiOperation(value = "客户详情查询", httpMethod = "POST")
    public TerminusResult<CustomInfoResult> getDetail(@RequestBody CustomInfoGetsForm form) {
        CustomInfoGetsParam param = BeanCopyUtils.copyProperties(form, CustomInfoGetsParam.class);
        try {
            return customInfoService.getDetail(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("查询客户详情查询失败，原因：", e);
            return TerminusResult.fail("410004", this.getMessage("410004"));
        }
    }


    @RequestMapping(value = "/addUserRecordInfo", method = RequestMethod.POST)
    @ApiOperation(value = "新增客户跟进记录", httpMethod = "POST")
    public TerminusResult<String> addUserRecordInfo(@RequestBody AddUserRecordInfoForm form) {
        AddUserRecordInfoParam param = BeanCopyUtils.copyProperties(form, AddUserRecordInfoParam.class);
        try {
            return customInfoService.addUserRecordInfo(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("新增客户跟进记录失败，原因：", e);
            return TerminusResult.fail("410005", this.getMessage("410005"));
        }
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "修改客户信息", httpMethod = "POST")
    public TerminusResult<Boolean> edit(@RequestBody CustomInfoEditForm form) {
        CustomInfoEditParam param = BeanCopyUtils.copyProperties(form, CustomInfoEditParam.class);
        try {
            return customInfoService.update(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("修改客户信息失败，原因：", e);
            return TerminusResult.fail("410006", this.getMessage("410006"));
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除客户信息", httpMethod = "POST")
    public TerminusResult<Boolean> delete(@RequestBody CustomInfoDeleteForm form) {
        CustomInfoDeleteParam param = BeanCopyUtils.copyProperties(form, CustomInfoDeleteParam.class);
        try {
            return customInfoService.delete(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("删除客户信息失败，原因：", e);
            return TerminusResult.fail("410007", this.getMessage("410007"));
        }
    }

    @RequestMapping(value = "/belongTo", method = RequestMethod.POST)
    @ApiOperation(value = "分配归属", httpMethod = "POST")
    public TerminusResult<Boolean> belongTo(@RequestBody CustomBelongtoForm form) {
        CustomBelongtoParam param = BeanCopyUtils.copyProperties(form, CustomBelongtoParam.class);
        try {
            return customInfoService.belongTo(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("分配归属失败，原因：", e);
            return TerminusResult.fail("410008", this.getMessage("410008"));
        }
    }

    @RequestMapping(value = "/getNameListByName", method = RequestMethod.POST)
    @ApiOperation(value = "根据客户名返回10条客户列表", httpMethod = "POST")
    public TerminusResult<List<CustomInfoByNameResult>> getNameListByName(@RequestBody GetNameListByNameForm form) {
        GetNameListByNameParam param = BeanCopyUtils.copyProperties(form, GetNameListByNameParam.class);
        try {
            return customInfoService.getNameListByName(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("根据客户名返回10条客户列表，原因：", e);
            return TerminusResult.fail("410009", this.getMessage("410009"));
        }
    }

    @RequestMapping(value = "/getChannelCustomerNumber", method = RequestMethod.POST)
    @ApiOperation(value = "获取渠道联系人客户数", httpMethod = "POST")
    public TerminusResult<List<ChannelCustomerNumberResult>> getChannelCustomerNumber(@RequestBody CustomerChannelUserForm form) {
        CustomerChannelUserParam param = BeanCopyUtils.copyProperties(form, CustomerChannelUserParam.class);
        try {
            return customInfoService.getChannelCustomerNumber(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("获取渠道联系人客户数失败，原因：", e);
            return TerminusResult.fail("410019", this.getMessage("410019"));
        }
    }

    @RequestMapping(value = "/analysisStage", method = RequestMethod.POST)
    @ApiOperation(value = "客户招商阶段统计分析", httpMethod = "POST")
    public TerminusResult<List<CustomerStageNumResult>> analysisStage(@RequestBody CustomerStageQuery query) {
        CustomerStageParam param = BeanCopyUtils.copyProperties(query, CustomerStageParam.class);
        try {
            return customInfoService.analysisStage(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("客户招商阶段统计分析数据失败，原因：", e);
            return TerminusResult.fail("410017", this.getMessage("410017"));
        }
    }

    @RequestMapping(value = "/analysisNatureAndIndustry", method = RequestMethod.POST)
    @ApiOperation(value = "客户性质和行业分析", httpMethod = "POST")
    public TerminusResult<CustomerNatureIndustryResultList> analysisNatureAndIndustry(@RequestBody CustomerStageQuery form) {
        CustomerStageParam param = BeanCopyUtils.copyProperties(form, CustomerStageParam.class);
        try {
            return customInfoService.analysisNatureAndIndustry(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("获取客户性质和行业分析数据失败，原因：", e);
            return TerminusResult.fail("410021", this.getMessage("410021"));
        }
    }

}
